;;; -*- Mode:Lisp; Coding:utf-8; -*-
;;;
;;; 説明:
;;; Maclispの入出力系ユーティリティの模倣
;;;
;;; オリジナルは、
;;; ftp://ftp.ultimate.com/pdp10/maclisp/lisp.tar.gz
;;; より入手可能。

(in-package #:maclisp-compat.setagaya.mc)
(export '(iota 
	  phi 
	  make-input-stream
	  make-output-stream))

(defmacro iota ((&rest stream-binds) &body body)
  (let ((abortp (gensym)))
    `(let (,@(mapcar (lambda (x)
		       `(,(car x) (open ,(cadr x) ,@(cddr x))))
		     stream-binds)
	   (,abortp t))
       (unwind-protect
	    (multiple-value-prog1
		(progn ,@body)
	      (setq ,abortp nil))
	 (progn
	   ,@(mapcar (lambda (x) `(when ,(car x) (close ,(car x) :abort ,abortp)))
		     stream-binds))))))

(defmacro phi ((&rest stream-binds) &body body)
  (let ((abortp (gensym)))
    `(let (,@stream-binds (,abortp t))
       (unwind-protect
	    (multiple-value-prog1
		(progn ,@body)
	      (setq ,abortp nil))
	 (progn
	   ,@(mapcar (lambda (x) `(when ,(car x) (close ,(car x) :abort ,abortp)))
		     stream-binds))))))

(defmacro make-input-stream (path &rest keys)
  `(open ,path :direction :input ,@keys))

(defmacro make-output-stream (path &rest keys)
  `(open ,path :direction :output ,@keys))